package com.example.hospitalsystem.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.hospitalsystem.mapper.CommentMapper;
import com.example.hospitalsystem.mapper.DesignMapper;
import com.example.hospitalsystem.mapper.UserMapper;
import com.example.hospitalsystem.pojo.dto.comment.*;
import com.example.hospitalsystem.pojo.entity.Comments;
import com.example.hospitalsystem.pojo.entity.Design;
import com.example.hospitalsystem.pojo.entity.Likes;
import com.example.hospitalsystem.pojo.entity.User;
import com.example.hospitalsystem.result.Result;
import com.example.hospitalsystem.service.CommentService;
import com.example.hospitalsystem.utils.JwtHelper;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.stream.events.Comment;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comments> implements CommentService {

    @Autowired
    private HttpServletRequest request;

    @Autowired
    private JwtHelper jwtHelper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DesignMapper designMapper;

    @Override
    public Result<Object> getAllCommentInfo(CommentPageQueryDto commentPageQueryDto) {
        Page<Comments> commentsPage=new Page<>(commentPageQueryDto.getPageNum(),commentPageQueryDto.getPageSize());

        if(!commentPageQueryDto.getContent().isEmpty()){
            LambdaQueryWrapper<Comments> commentsLambdaQueryWrapper=new LambdaQueryWrapper<>();

            IPage<Comments> commentsIPage=baseMapper.selectPage(commentsPage,commentsLambdaQueryWrapper);
            return Result.build(commentsIPage,200,"返回所有评论信息成功");
        }

        IPage<Comments> commentsIPage=baseMapper.selectPage(commentsPage,null);
        return Result.build(commentsIPage,200,"返回所有评论信息成功");    }

    @Override
    public Result<Object> addCommentInfo(AddCommentDto addCommentDto) {

        String token=request.getHeader("Authorization");
        int userId=jwtHelper.getUserId(token);

        LambdaQueryWrapper<User> userLambdaQueryWrapper=new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getUserId,userId);

        User user=userMapper.selectOne(userLambdaQueryWrapper);

        Comments comments=new Comments();
        if(addCommentDto.getParentCommentId()!=0){
            comments.setParentCommentId(addCommentDto.getParentCommentId());
        }

        LocalDateTime time= LocalDateTime.now();
        comments.setCommentTime(time);
        comments.setDesignId(addCommentDto.getDesignId());
        comments.setCommentText(addCommentDto.getCommentText());
        comments.setUserId(userId);
        comments.setUsername(user.getUsername());
        comments.setImage(user.getImage());
        this.save(comments);

        return Result.build(comments,200,"添加评论信息成功");
    }

    @Override
    public Result<Object> deleteCommentInfo(DeleteCommentDto deleteCommentDto) {
        LambdaQueryWrapper<Comments> commentsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        commentsLambdaQueryWrapper.eq(Comments::getCommentId,deleteCommentDto.getCommentId())
                .eq(Comments::getDesignId,deleteCommentDto.getDesignId());
        int i=baseMapper.delete(commentsLambdaQueryWrapper);
        if(i<0){
            return Result.build("删除评论失败");
        }
        return Result.build("删除评论成功");
    }

    @Override
    public Result<Object> getAllCommentByDesign(int designId) {
        List<CommentsDto> commentsDtos=baseMapper.selectCoomment(0,designId);
        commentsDtos.forEach(this::setChildrenByDesignId);
        return Result.build(commentsDtos,200,"返回设计下的所以评论信息成工功");
    }

    @Override
    public Result<Object> getAllCommentByParentAndDesign(GetCommentByDesignAndParentDto getCommentByDesignAndParentDto) {
        LambdaQueryWrapper<Comments> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Comments::getParentCommentId,getCommentByDesignAndParentDto.getCommentParentId())
                .eq(Comments::getDesignId,getCommentByDesignAndParentDto.getDesignId());

        List<Comments> commentsList=baseMapper.selectList(lambdaQueryWrapper);
        return Result.build(commentsList,200,"返回设计中父评论下的所有子评论信息成功");
    }

    @Override
    public Result<Object> lookCommentInfo() {
        String token=request.getHeader("Authorization");
        int userId=jwtHelper.getUserId(token);
        LambdaQueryWrapper<Comments> commentsLambdaQueryWrapper=new LambdaQueryWrapper<>();
        commentsLambdaQueryWrapper.eq(Comments::getUserId,userId);

        List<Comments> commentsList=baseMapper.selectList(commentsLambdaQueryWrapper);

        List<Design> designList=new ArrayList<>();
        for(Comments comments:commentsList){
            Design design=new Design();
            LambdaQueryWrapper<Design> designLambdaQueryWrapper=new LambdaQueryWrapper<>();
            designLambdaQueryWrapper.eq(Design::getDesignId,comments.getDesignId());
            design=designMapper.selectOne(designLambdaQueryWrapper);
            designList.add(design);
        }
        return Result.build(designList,200,"返回所有评论信息成功");
    }

    public void setChildrenByDesignId(CommentsDto commentsDto){

        List<CommentsDto> list=baseMapper.selectCoomment(commentsDto.getCommentId(),commentsDto.getDesignId());
        if(!list.isEmpty()){
            commentsDto.setChildren(list);
            list.forEach(this::setChildrenByDesignId);
        }
    }
}
